﻿using System;
using System.Data;
using System.Configuration; 
using System.Text;
using System.Net.Mail;
using System.Collections.Generic;
using System.Net.Sockets;
using System.IO;
using System.Threading;

/// <summary>
/// SendMail 的摘要说明
/// </summary>
public class SendMail
{
    public SendMail()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    #region 发送邮件

    /// <summary>
    /// 发送邮件

    /// </summary>
    /// <param name="to">要发送到的邮件地址 string to</param>
    /// <param name="from">发送人邮件地址 string from</param>
    /// <param name="subject">邮件标题 string subject</param>
    /// <param name="body">邮件正文 string body</param>
    /// <returns>该方法返回的参数表示是否发送成功 string result</returns>
    //public string Send(string to, string from, string subject, string body)
    //{
    //    //MailMessage Message = new MailMessage();
    //    //string UserName = ConfigurationSettings.AppSettings["EmailUserName"].Trim().ToString();
    //    //string Password = ConfigurationSettings.AppSettings["EmailPassword"].Trim().ToString();
    //    //string Server = ConfigurationSettings.AppSettings["EmailServer"].Trim().ToString();
    //    //try
    //    //{
    //    //    Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//
    //    //    Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", UserName);//UserName
    //    //    Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Password);//Password
    //    //    Message.To = to;
    //    //    Message.From = from;
    //    //    Message.Subject = subject;
    //    //    Message.Body = body;

    //    //    SmtpMail.SmtpServer = Server;
    //    //    SmtpMail.Send(Message);
    //    //}
    //    //catch (System.Web.HttpException ehttp)
    //    //{
    //    //    Console.Write(ehttp.ToString());
    //    //    return "NO";
    //    //}
    //    //return "Yes";
    //}
    #endregion

    /// <summary>
    /// 发送邮件

    /// </summary>
    /// <param name="to">要发送到的邮件地址 string to</param>
    /// <param name="from">发送人邮件地址 string from</param>
    /// <param name="subject">邮件标题 string subject</param>
    /// <param name="body">邮件正文 string body</param>
    /// <returns></returns>
    public static bool sendMail(string to, string from, string subject, string body)
    {   
        try
        {
            EmailContents = new EmailContentPara(to, from, subject, body);
            Thread mailThread = new Thread(new ThreadStart(ExeEmail));
            mailThread.Start();
            return true;
        }
        catch { return false; }
    }


    private static void ExeEmail()
    { 
        if (EmailContents!=null)
        {
            lock (EmailContents)
            {      
                //string strHost = ConfigurationManager.AppSettings["EmailServer"].ToString();
                //string strAccount = ConfigurationManager.AppSettings["EmailUserName"].ToString();
                //string strPwd = ConfigurationManager.AppSettings["EmailPassword"].ToString();

                //SmtpClient sc = new SmtpClient();
                //sc.DeliveryMethod = SmtpDeliveryMethod.Network;
                //sc.Host = strHost;
                
                //sc.EnableSsl = true;
                //sc.Credentials = new System.Net.NetworkCredential(strAccount, strPwd);
                //MailMessage mm = new MailMessage(EmailContents.From, EmailContents.To);
                ////mm.BodyEncoding.
                //mm.Subject = EmailContents.Subject;
                //mm.Body = EmailContents.Body;
                //mm.BodyEncoding = System.Text.Encoding.UTF8;
                //mm.SubjectEncoding = System.Text.Encoding.UTF8;
                //mm.IsBodyHtml = true;
                //mm.Priority = MailPriority.High;
                //sc.Send(mm);

                //
                try
                {
                    MailMessage m_message = new MailMessage();
                    m_message.From = new MailAddress(EmailContents.From);
                    m_message.To.Add(new MailAddress(EmailContents.To));
                    m_message.Subject = EmailContents.Subject;
                    m_message.IsBodyHtml = true;
                    m_message.Body = EmailContents.Body;
                    SmtpClient m_smtpClient = new SmtpClient();
                    m_smtpClient.EnableSsl = false;
                    m_smtpClient.Send(m_message);
                }
                catch(Exception ee)
                {
                   // GiftNet.AppBLL.AppManager.BaseManger.WriteLogFile(ee.Message + ",Email To:" + EmailContents.To + ",Email content:" + EmailContents.Body);
                }

            }
        }
        EmailContents = null;
    }

    public static EmailContentPara _EmailContents;
    public static EmailContentPara EmailContents
    {
        get
        {
            return _EmailContents;
        }
        set
        {
            _EmailContents = value;
        }
    }


    public  class EmailContentPara
    {
        public string To;//
        public string From;
        public string Subject;
        public string Body;

        public EmailContentPara(string to, string from, string subject, string bady)
        {
            this.To = to;
            this.From = from;
            this.Body = bady;
            this.Subject = subject;
        }
    }
}
